package com.t.bricks.bussiness.service.sendmsg.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.sendmsg.SendMsgDao;
import com.t.bricks.bussiness.db.entity.sendmsg.SendMsg;
import com.t.bricks.bussiness.service.sendmsg.SendMsgService;
import com.t.bricks.model.MsgEmity;

/**
 * 发送消息管理'SendMsg表'基本业务操作类接口实现
 */
@Service
public class SendMsgServiceImpl implements SendMsgService<SendMsg> {

	@Autowired
	private SendMsgDao<SendMsg> sendMsgDao;
	
	/**
	 * 查找待发送的对象集合, 取'待发送'及'发送中'状态维持了10分钟的
	 * @param iErrorMax 错误次数上限
	 * @return
	 */
	@Override
	public MsgEmity findByWait(Integer iErrorMax) {
		if (null == iErrorMax || iErrorMax.intValue() < 1) {
			return new MsgEmity(false, "错误阈值不能小于1！", 8001);
		}
				
		return sendMsgDao.findByWait(iErrorMax);
	}
	
	/**
	 * 累加错误次数
	 * @param sId
	 * @param iErrorMax
	 * @param sMemo
	 * @return
	 */
	@Override
	public MsgEmity addError(String sId, Integer iErrorMax, String sMemo) {
		if (null == sId || "".equals(sId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		if (null == iErrorMax || iErrorMax.intValue() < 1) {
			return new MsgEmity(false, "错误阈值不能小于1！", 8002);
		}
		
		if (iErrorMax.intValue() > 1000) {
			return new MsgEmity(false, "错误阈值不能大于1000！", 8002);
		}
		
		return sendMsgDao.addError(sId, iErrorMax, sMemo);
	}
	
	/**
	 * 错误次数清零
	 * @param sId
	 * @return
	 */
	@Override
	public MsgEmity clearError(String sId) {
		if (null == sId || "".equals(sId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		return sendMsgDao.clearError(sId);
	}
	
}
